package com.mec.beans.y2013.util;

import java.util.Random;

import org.apache.log4j.Logger;

/**
 * String Utilities;
 * @author MEC
 * @since 2013-03-29
 */
public class StringUtil {
	
	private StringUtil(){
		rand = new Random();
		strLowerLetters = getLowerLetters();
		strUpperLetters = getUpperLetters();
		strNumbers = getNumbers();
		strSequence = strLowerLetters + strUpperLetters + strNumbers;
	}
	
	public static StringUtil getInstance(){
		return instance;
	}
	
	protected String getStrSequence(){
		return strSequence;
	}
	
	private static String getLowerLetters(){
		String retval = "";
		
		char a = 'a';
		char z = 'z';
		
		for (char i = a; i <= z; ++i){
			retval += i;
		}
		
		return retval;
	}
	
	private static String getUpperLetters(){
		String retval = "";
		char A =  'A';
		char Z = 'Z';
		
		for (char i = A; i <= Z; ++i){
			retval += i;
		}
		
		return retval;
	}
	
	private static String getNumbers(){
		String retval = "";
		char i0 = '0';
		char i9 = '9';
		
		for (char i = i0; i<= i9; ++i){
			retval += i;
		}
		
		return retval;
	}
	
	/**
	 * Return a string constructed by random character/numericals with given length;
	 * INPUT:
	 * 1, 5
	 * 2, 
	 * OUTPUT:
	 * 1, "14332" (random number str);
	 * 2,
	 * @param length length of the random characters string;
	 * @return
	 */
	public String getRandStr(int length){
		if(0 >= length){
			return "";
		}
		
		StringBuilder retval = new StringBuilder();
		int len = strSequence.length();
		for(int i = 0; i < length; ++i){
			retval.append(strSequence.charAt(rand.nextInt(len))); 
		}
		
		return retval.toString();
	}
	
	private boolean initialized;
	private Random rand;
	private String strSequence; //abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
	private String strLowerLetters; //abcdefghijklmnopqrstuvwxyz
	private String strUpperLetters; //ABCDEFGHIJKLMNOPQRSTUVWXYZ
	private String strNumbers; //0123456789
	
	private static final StringUtil instance = new StringUtil();
	private static final Logger logger = Logger.getLogger(StringUtil.class);

}
